var app = angular.module('businessApp', []);
var keyTip="请输入待检索的关键字";

app.controller('businessController', function($scope, $http, BusinessService) {
	$scope.load=function(isRest){
		$scope.sel=$scope.sel?$scope.sel:{};
		var pageNum=$scope.pageInfo?isRest==false?$scope.pageInfo.pageNum:1:1;
		var pageSize=$scope.pageInfo?$scope.pageInfo.pageSize:10;
		$scope.sel.pageNum=pageNum;
		$scope.sel.pageSize=pageSize;
		var param={};  
        for(var k in $scope.sel){ 
        	param[k] = $scope.sel[k];  
        	if(k=="key"&&$scope.sel[k]==keyTip){
        		delete param[k];
        	}
        } 
		BusinessService.list(param).success(function (response) {
			$scope.pageInfo=response.data;
			$scope.pageInfo.pageLinks=getPageLinks(response.data.pageNum,response.data.pages);
		});
	
	}
	
	$scope.load();
    $scope=pagination.init($scope);
    $scope.businessCreator=function(){
    	redirectUrl('manage/business/','add.jsp');
    }
    
    $scope.refreshData=function(){
    	$scope.load();
    }
   /*
    $.ajax({ 
    		cache : true,
    		type : "POST",
    		url : '',
    		data :{'id':name},
    		async : false,
    		error : function(response) {
    			
    		},
    		success : function(response) {
    			
    		}
    	
    	});
    
    */
    $scope.delTip=function(id,mes){
    	
    	$scope.delItem={id:id,mes:mes};
    	$('#box-del').modal({
    		show: true
    	});
    	
    }
    $scope.del=function(id){
    	BusinessService.del(id).success(function (response) {
    		if (response.meta.success==true) {
    			$('#box-del').modal('hide');
    			$scope.load();
    		} else {
    			$('#box-dels').modal({
    	    		show: true
    	    	});
    			$('#box-del').modal('hide');
    		}
    	});
    }


    if(inputPlaceholder()){
    	$scope.sel.key=keyTip;
    }



});

app.factory('BusinessService', function ($http) {
	var list = function (param) {
        return $http({
            params: param,
            url: timestamp('qz/business/getByPage')
        });
    };
    var lock = function (id){
    	return $http.put('qz/business/lock/'+id);
    }
    var del = function (id){
    	return $http.put('qz/business/upStatus/'+id);
    }
	return {
		list:function(param){
			return list(param);
		},
		lock:function(id){
			return lock(id);
		},
		del:function(id){
			return del(id);
		}
	}
});
